<?php if ( ! defined('APPPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
class pin_controller extends PZ_Controller{

	public function __construct() {
		parent::__construct();
		$this->authfunctions = array('');
		$settingapi = load_api('setting');
		$this->colorsetting = $settingapi->getSetting('colorindex');
		$this->sharebuttons = $settingapi->getSetting('sharebutton');
		$this->layoutsetting = $settingapi->getSetting('layoutpin');
		$this->timesort = ($this->layoutsetting['uipin_timesort'])?$this->layoutsetting['uipin_timesort']:'create_time';
	}

	private function checklayout(){
		if($type = $this->input->cookie(config_item('cookie_prefix').'pz_layout_pin')){
			$this->pinlayout=$type;
		}else{
			$this->pinlayout='wf';
		}
	}

	public function init(){
		$this->checklayout();
	}

	public function index(){
		$this->load_seo('category');
		$this->output_pin('index', " ptx_share.{$this->timesort} DESC ");
	}

	public function color(){
		$color=$this->param("cdx");
		$conditions['color'] = $color;
		$this->cdx=$color;

		$this->seo_title(lang($color,'color'));
		$this->seo_keyword(lang($color,'color'));
		$this->seo_description(lang($color,'color'));

		$this->output_pin('color', " ptx_share.{$this->timesort} DESC ",$conditions);
	}


	public function hot(){
		$this->output_pin('hot', " ptx_share.total_like DESC ");
	}

	public function lastest(){
		$this->output_pin('lastest', " ptx_share.{$this->timesort} DESC ");
	}

	public function goods(){
		$conditions['type'] = 'goods';
		$this->output_pin('goods', " ptx_share.{$this->timesort} DESC ",$conditions);
	}

	public function video(){
		$conditions['type'] = 'video';
		$this->output_pin('video', " ptx_share.{$this->timesort} DESC ",$conditions);
	}

	public function share(){
		$conditions['type'] = 'share';
		$this->output_pin('share', " ptx_share.{$this->timesort} DESC ",$conditions);
	}

	public function article(){
		if($this->layoutsetting['uipin_layout']=='ls_article'){
			$this->pinlayout='ls';
		}
		$conditions['type'] = 'article';
		$this->output_pin('article', " ptx_share.{$this->timesort} DESC ",$conditions);
	}

	private function output_pin($action,$order,$conditions=array()){
		$shareapi = load_api('share');
		$categoryapi = load_api('category');
		$setting = $this->layoutsetting;
		$pagesize = $setting['uipin_numperpage']?$setting['uipin_numperpage']:15;
		if(!in_array($conditions['type'], array('goods','video','share','article'))){
			$conditions['type'] = ($setting['uipin_showarticle']||$this->pinlayout=='ls')?'all':'noarticle';
		}


		$numbershowpage = $setting['uipin_numbershowpage']?$setting['uipin_numbershowpage']:0;

		if($catid=$this->param('cat')){
			$data['category_id'] = $catid;
		}elseif($catname = $this->param('name')){
			$category = $categoryapi->getCategoryByENName($catname);
			$data['category_id'] = $category['category_id'];
			$data['category_name_en'] = $catname;
		}

		$data['subcategory_id'] = $this->param('subcat');

		$args = array("page"=>"2");
		if($this->pinlayout=='wf'){
			$args['wf'] = '1';
		}

		$page = $this->param('page','1');
		$wpage = $this->param('wpage');
		if($wpage&&$numbershowpage){
			$data['page'] = ($wpage-1)*$numbershowpage+$page;
			$args['wpage'] = $data['wpage'] = $wpage;
		}else{
			$data['page'] = $page;
		}
		$data['showpage'] = (($numbershowpage)&&($data['page']%$numbershowpage==0))?'1':'0';

		if($range = $this->param('range')){
			$ranges = explode('~', $range);
			$data['range'] = $args['range'] = $range;
			$conditions['lowerprice'] = $ranges[0];
			$conditions['higherprice'] = $ranges[1];
		}

		if($data['category_id']){
			$category = $category?$category:$categoryapi->getCategoryByid($data['category_id'],true);
			$this->navmenu = ($category['parent_name'])?$category['parent_name'].'-'.$category['category_name_cn']:$category['category_name_cn'];
			$guide['hottags'] = $categoryapi->getCategoryHotTags($data['category_id']);
			$this->seo_title($this->navmenu);
			$this->seo_description($this->navmenu);
			$this->seo_keyword(implode(' ', $guide['hottags']).' '.$this->navmenu);
		}

		if($data['page'] == 1){
			$guide['show'] = true;
			if($data['category_id']){
				$guide['subcats'] = $categoryapi->getSubCategories($data['category_id']);
			}
			$data['guide'] = $guide;
			$topshares = $shareapi->getTopShares($data['category_id']);
			$topshares = $shareapi->prepareShares($topshares);
		}

		if($data['category_id']){
			if($data['subcategory_id']){
				//$conditions['category_id'] = $data['subcategory_id'];
				$args['subcat']=$data['category_id'];
			}else{
				/* if($guide['subcats']){
					$conditioncats[] = $data['category_id'];
				foreach ($guide['subcats'] as $sub){
				$conditioncats[] = $sub['category_id'];
				}
				$conditions['category_in'] = $conditioncats;
				}else{
				$conditions['category_id'] = $data['category_id'];
				} */
			}
			$ids = array($data['category_id']);
			$categoryapi->get_subcat_ids($data['category_id'], $ids);
			$conditions['category_in'] = $ids;
			$args['cat']=$data['category_id'];
		}

		if($tag = $this->param('tag')){
			$tagafter = pzxdecode($tag);
			$conditions['keyword'] = $args['tag'] = $tag;
			$this->navmenu = $data['tag'] = $tagafter;

			$this->seo_title($tagafter);
			$this->seo_keyword($tagafter);
			$this->seo_description($tagafter);
		}
		if($cdx = $this->param('cdx')){
			$args['cdx'] = $cdx;
		}
		if($keyword= $this->param('keyword')){
			if($this->isChineseVersion()){
				$keyword = safeEncoding($keyword);
			}
			$conditions['keyword'] = pzxencode($keyword);
			$args['keyword']=$keyword;
			$this->navmenu = $keyword;
			$this->seo_title($keyword);
			$this->seo_keyword($keyword);
			$this->seo_description($keyword);
		}
		if(!$setting['uipin_showforward']){
			$conditions['orgin_post']=1;
		}
		$conditions['notop'] = true;
		$shares = $shareapi->search($conditions,$data['page'],$pagesize,$order);
		$shares = $shareapi->prepareShares($shares);
		if($setting['uipin_ads']&&$this->pinlayout=='wf'){
			$shares = $shareapi->add_ads($shares);
		}
		if($topshares){
			$shares = array_merge($topshares,$shares);
		}
		$data['nextpage_url'] = site_url_param('pin/'.$action,$args);
		if($data['category_name_en']){
			unset($args['cat']);
			$args['name'] = $data['category_name_en'];
		}
		$pager = &load_class('Pager', 'core');
		unset($args['wf']);
		$data['pages'] = $pager->createPages('pin/'.$action,$args);
		unset($args['page']);
		$data['wpages'] = $pager->createCustomPages('pin/'.$action,$args,'wpage',$pagesize*$numbershowpage);

		if($this->pinlayout=='ls'){
			if(!$guide['subcats']){
				$data['subcats'] = ($data['category_id'])?$categoryapi->getSubCategories($data['category_id']):$categoryapi->getRootCategroiesFromDb();
			}else{
				$data['subcats'] = $guide['subcats'];
			}
			$data['shares'] = $shares;
			$this->output("pin/list",$data,true);
		}else{
			$data['tpl_waterfall'] = $this->render('waterfall/pin',array('shares'=>$shares,'showpage'=>$data['showpage']));
			$data['pinsetting'] = $setting;
			$this->output("pin/index",$data,($this->param('wf'))?false:true);
		}
	}

}



